package com.hisense.ovcloud.common3.base.handlers;

import com.hisense.ovcloud.common3.constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.LinkedHashMap;
import java.util.Map;

@Component
@Slf4j
public class RestExceptionHandler extends DefaultErrorAttributes {
    public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
        return assembleError(request);
    }

    private Map<String, Object> assembleError(ServerRequest request) {
        Map<String, Object> errorAttributes = new LinkedHashMap<>();
        Throwable e = getError(request);
        log.error("Exception occured: {}", e.getMessage(), e);

        errorAttributes.put("code", Constants.BASE_RESPONSE.CODE_FAILED);
        errorAttributes.put("data", null);
        errorAttributes.put("errorMsg", e.getMessage());
        return errorAttributes;
    }
}
